const { Http } = require("../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {

    cardBase: {
      //需要https图片路径,下载到本地然后去绘制 
      cardbg: "https://img01.yzcdn.cn/upload_files/2021/03/31/Fl4o_RUoMVw8t69WPEM3lZLUd-Js.jpg",
      // 二维码
      codeImg: "",
    },
    bgimg: "",
    codeimg: "",
    shareImagePath: "",
    isPhotoModel: "",
    isCanvas: "",
    id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async  function (options) {
    
    this.setData({
      id:options.id
    })
    console.log(this.data.id);
    wx.showLoading({
      title: '正在生成海报...',
      mask: true
    })
    let result ;
    if(this.data.id>0){
      console.log(this.data.id);
     
       result = await Http.request({
        url: 'wechat/order/get_qrcode_by_pages',
        data: { pages: "pages/home/index" ,scene: wx.getStorageSync("user_id")+","+ wx.getStorageSync("store_id")+","+this.data.id+",0" ,force:1},
        method: "POST"
      });
    }else{
      console.log(this.data.id);
       result = await Http.request({
        url: 'wechat/order/get_qrcode_by_pages',
        data: { pages: "pages/home/index" ,scene: wx.getStorageSync("user_id")+","+ wx.getStorageSync("store_id")+","+ wx.getStorageSync("store_id")+",1" ,force:1},
        method: "POST"
      });
    }
   
    console.log(result.data.qrcode);
    if(result.code == 0){
      this.data.cardBase.codeImg = result.data.qrcode;
      // console.log(this.data.cardBase.codeImg);
      this.cardbg();
    }else{
      console.log('操作请重试');
    }
    console.log(result);
    // this.cardbg();
  },
  cardbg: function () {
    var that = this;
    wx.getImageInfo({
      src: that.data.cardBase.cardbg,
      success(res) {
        that.setData({
          bgimg: res.path
        })
        console.log("得到背景图片");
        that.codeImg();
      }, fail(err) {
        console.log("未得到背景图片");
      }
    })
  },
  codeImg: function () {
    var that = this;
    console.log(that.data.cardBase.codeImg);
    wx.getImageInfo({
      src: that.data.cardBase.codeImg,
      success(res) {
        that.setData({
          codeimg: res.path
        })
        console.log(res.path);
        console.log("得到二维码");
        that.createNewImg()
      }, fail(err) {
        console.log("未得到背景图片");
      }
    })
  },
  setQrcode(context) {
    let path = this.data.codeimg //小程序码
    context.drawImage(path, 200,440, 90, 92)
  },
  //将canvas转换为图片保存到本地，然后将图片路径传给image图片的src
  createNewImg() {
    let context = wx.createCanvasContext('mycanvas')
    let path = this.data.bgimg
    let that = this
    console.log(that.data.bgimg);
    context.drawImage(path, 0, 0, 318, 565) //以iPhone 6尺寸大小为标准绘制图片
    that.setQrcode(context)
    //绘制图片
    context.draw()
    context.save()
    //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
    setTimeout(() => {
      wx.canvasToTempFilePath({
        canvasId: 'mycanvas',
        success: function (res) {
          wx.hideLoading()
          that.setData({
            shareImagePath: res.tempFilePath
          })
        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      }, this)
    }, 2000)
    wx.hideLoading();
  },
  savePhoto() {
    var that = this
    wx.showLoading({
      title: '正在保存...',
      mask: true
    })
    setTimeout(() => {
      wx.saveImageToPhotosAlbum({
        filePath: that.data.shareImagePath,
        success(res) {
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
          setTimeout(() => {
            wx.hideLoading()
            that.setData({
              isPhotoModel: false,
              isCanvas: false
            })
          }, 300)
        },
        fail() {
          wx.showToast({
            title: '保存失败，请刷新页面重试',
            icon: 'none'
          })
          setTimeout(() => {
            wx.hideLoading()
            that.setData({
              isPhotoModel: false,
              isCanvas: false
            })
          }, 300)
        }
      })
    }, 2500)
  }
})